package com.example.learnvideo.adapter

import android.app.Activity
import android.content.Context
import android.media.Image
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.example.learnvideo.R
import com.example.learnvideo.bean.WifiConnect
import com.example.learnvideo.widget.InputDialogFragment

/**
 *
 * @Author： LJH
 * @Time： 2022/7/21
 * @description：
 */
class WifiListAdapter(context:Context,wifiManager: WifiManager,wifiList:ArrayList<WifiConnect>):RecyclerView.Adapter<WifiListAdapter.ViewHolder>() {

    private val mContext = context
    private val mWifiManager:WifiManager = wifiManager
    private val mWifiList:ArrayList<WifiConnect> = wifiList
    private val mSignalList = arrayOf(
        R.drawable.signal_1, R.drawable.signal_2, R.drawable.signal_3, R.drawable.signal_4
    )

    //初始化适配器view控件
    inner class ViewHolder(view:View):RecyclerView.ViewHolder(view){
        val ckWifi = view.findViewById<CheckBox>(R.id.ck_wifi)
        val ivWifi = view.findViewById<ImageView>(R.id.iv_wifi)
    }

    //关联适配器布局
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WifiListAdapter.ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_wifi,parent,false)
        return ViewHolder(view)
    }

    //填充数据
    override fun onBindViewHolder(holder: WifiListAdapter.ViewHolder, position: Int) {
        val client = mWifiList[position]
        holder.ckWifi.text = client.ssid
        holder.ckWifi.isChecked = client.status
        holder.ckWifi.setOnCheckedChangeListener { buttonView, isChecked ->
            run {
                if (isChecked) {
                    if (client.networkId >= 0) {//已配对过的wifi
                        Log.d("checked", "连接已配对过的wifi")
                        mWifiManager.enableNetwork(client.networkId, true)
                    } else {
                        //不需要密码的wifi
                        if (client.type == 0) {
                            Log.d("checked", "连接不需要密码的wifi")
                            val config = WifiConfiguration()
                            config.SSID = "\"${client.ssid}\""
                            config.wepKeys[0] = ""
                            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
                            config.wepTxKeyIndex = 0
                            val netId = mWifiManager.addNetwork(config)
                            mWifiManager.enableNetwork(netId, true)
                        } else { //需要密码的wifi
                            Log.d("checked", "连接需要密码的wifi")
                            val dialog = InputDialogFragment.newInstance(
                                client.ssid,
                                client.type,
                                "请输入wifi密码"
                            )
                            val fragTag = mContext.resources.getString(R.string.app_name)
                            dialog.show((mContext as Activity).fragmentManager, fragTag)
                        }
                    }
                } else {
                    mWifiManager.disconnect()
                }
            }
        }
        holder.ivWifi.setImageResource(mSignalList[client.level])
    }

    override fun getItemCount(): Int = mWifiList.size
}